package cn.jekun.web.shop.service;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.D;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.LsLog;
import cn.jekun.component.core.bean.gen.LsLogType;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.service.ComponentCoreService;
import cn.jekun.web.shop.bean.TerminalLog;

@Service(TerminalLogService.BEAN_NAME)
public class TerminalLogService extends ComponentCoreService{
	public static final String BEAN_NAME="cn.jekun.web.shop.service.TerminalService";
	public static final String NAMESPACE="ACTION_LS_LOG";
	@Override
	public String getNamespace() {
		return NAMESPACE;
	}

	
	
	
	public List<TerminalLog> queryTerminalLogList(@D String beginDate,@D String endDate,
											 	  @LsLog.AnnoTerminalId String terminalId,@LsLogType.AnnoLogTypeId String type,
											 	  @RsUser.AnnoUserName String userName,@RsUser.AnnoRealName String realName,
											 	  @RsUser.AnnoShopId String shopId, Page page){
//		Map formMap=new HashMap<String, String>();
//		formMap.put("CREATE_TIME_beginDate", beginDate);
//		formMap.put("CREATE_TIME_endDate", endDate);
//		SqlUtis.convertBetweenValue(formMap, TerminalLog.CREATE_TIME);
//		formMap.put(RsTerminal.COL_TERMINAL_ID, terminalId);
//		formMap.put(LsLog.COL_LOG_TYPE_ID, type);
//		formMap.put(RsUser.COL_USER_NAME, userName);
//		formMap.put(RsUser.COL_REAL_NAME, realName);
//		formMap.put(ErpShop.COL_SHOP_ID,shopId);
//		SqlUtis.prepareSql(formMap
//				, SqlUtis.getSQL(formMap,"t."+RsTerminal.COL_TERMINAL_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+LsLog.COL_LOG_TYPE_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+RsUser.COL_USER_NAME,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+ErpShop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+RsUser.COL_REAL_NAME,Types.VARCHAR,SqlUtis.LIKE),
//				SqlUtis.getSQL(formMap,"t."+LsLog.COL_CREATE_TIME,Types.DATE)
//				);
//		return this.list(formMap, page);
		
		String SQL = "";
		if("".equals(beginDate) && !"".equals(endDate)){
			SQL +=" and a.create_time"
					+ "<= to_date('"+beginDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(beginDate) && "".equals(endDate)){
			SQL +=" and a.create_time"
					+ ">= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(beginDate) && beginDate!=null 
				&& !"".equals(endDate) && endDate!=null){
			SQL +=" and a.create_time >= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')"
					+ " and a.create_time <= to_date('"+endDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}
		if(terminalId!=null && terminalId.toString().length()>0){
			SQL +=" and a.TERMINAL_ID like '%"+terminalId.toString().trim()+"%'"; 
		}
		if(type!=null && type.toString().length()>0){
			SQL +=" and a.LOG_TYPE_ID = '"+type.toString().trim()+"'"; 
		}
		if(userName!=null && userName.toString().length()>0){
			SQL +=" and c.USER_NAME like '%"+userName.toString().trim()+"%'"; 
		}
		if(realName!=null && realName.toString().length()>0){
			SQL +=" and c.REAL_NAME like '%"+realName.toString().trim()+"%'"; 
		}
		if(shopId!=null && shopId.toString().length()>0){
			SQL +=" and d.shop_id = '"+shopId.toString().trim()+"'"; 
		}
	    System.out.println("SQL="+SQL);
		Map map=new HashMap<String, String>();
//		map.put("shopId", shopId);
		SqlUtis.prepareSql(map,SQL);
		
		return this.list(map, page);
	}
}
